package handler

import (
	"context"
	"examination-management/srv/cmd/config"
	"examination-management/srv/cmd/model"
	pb "examination-management/srv/proto"
)

func (s *Server) CreateTestOption(_ context.Context, in *pb.CreateTestOptionReq) (*pb.CreateTestOptionResp, error) {
	var m model.TestManagement
	config.DB.Debug().Where("id = ?", in.TestManagementId).Limit(1).Find(&m)
	if m.Id == 0 {
		return &pb.CreateTestOptionResp{
			Message: "试题不存在",
			Code:    10001,
		}, nil
	}
	option := model.TestOption{
		TestManagementId: in.TestManagementId,
		Select:           in.Select,
		Option:           in.Option,
	}
	err := config.DB.Debug().Create(&option).Error
	if err != nil {
		return &pb.CreateTestOptionResp{
			Message: "添加失败",
			Code:    10002,
		}, nil
	}
	return &pb.CreateTestOptionResp{
		Message: "添加成功",
		Code:    200,
	}, nil
}
